// ========================================================================
// Copyright (c) 2022 ChangYou, All rights reserved.
// http://www.changyou.com/
// 
// Filename:    RenderingQueueReceiver.cs
// Time:        2022-08-22 21:10:58
// Author:      xiangjinbao
// Email:       xiangjinbao@cyou-inc.com
// Version:     v2022.0.1
// Description: RenderingQueueReceiver
// ========================================================================

using System.Collections.Generic;
using UnityEngine;

namespace ProjectTW.Foliage
{
    /// <summary>
    /// 处理细节渲染队列实例
    /// </summary>
    public class RenderingQueueReceiver
    {
        // 渲染对象列表
        [System.NonSerialized]
        private RenderingQueue[] m_RenderingQueue;

        [System.NonSerialized]
        internal RenderingQueue[] ThreadedWarmedRenderingCache;
        [System.NonSerialized]
        internal List<RenderingQueue> ThreadedColdRenderingCache = new List<RenderingQueue>();

        // Chunks列表
        private FoliageChunk[] m_ChunkNeighbors;

        internal Plane[] InternalReusableCameraPlanes = new Plane[6];

        public Transform Transform;
        public Camera Camera;

        private Vector2 m_LastCheckedPosition;
        private bool m_CheckedPosition;

        internal RenderingQueue[] RenderingQueue
        {
            get
            {
                if (ThreadedWarmedRenderingCache != null)
                {
                    RenderingQueue = ThreadedWarmedRenderingCache;
                    ThreadedWarmedRenderingCache = null;
                }

                return m_RenderingQueue;
            }
            set
            {
                if (m_RenderingQueue != value)
                {
                    if (m_RenderingQueue != null)
                    {
                        RenderingPipielineCore.CleanupQueueInstances(m_RenderingQueue);
                    }

                    m_RenderingQueue = value;
                }
            }
        }

        public FoliageChunk[] ChunkNeighbors
        {
            get
            {
                if (m_ChunkNeighbors == null)
                {
                    m_ChunkNeighbors = FoliageStandaloneUtility.GetFoliageChunksNeighbors(Transform.position - FoliageManager.Instance.transform.position, m_ChunkNeighbors);
                }

                return m_ChunkNeighbors;
            }
            set
            {
                m_ChunkNeighbors = value;
            }
        }

        /// <summary>
        /// 检查位置是否移动，如果移动超出目标日志则执行一下逻辑：
        /// 
        /// 1) 更新9宫格相邻Chunks
        /// 2) 更新并重新计算草的密度
        /// </summary>
        public void CheckPositionChange()
        {
            Vector2 tPosition2D = new Vector2(Transform.position.x, Transform.position.z);

            if (!m_CheckedPosition || Vector2.Distance(m_LastCheckedPosition, tPosition2D) >= 1 + FoliageMeshInstance.Generation_Safe_Distance)
            {
                m_ChunkNeighbors = FoliageStandaloneUtility.GetFoliageChunksNeighbors(Transform.position - FoliageManager.Instance.transform.position, m_ChunkNeighbors);

                // 创建渲染队列实例
                CreateRenderQueue(m_CheckedPosition);
                m_LastCheckedPosition = tPosition2D;
                m_CheckedPosition = true;
                OnUpdated();
            }
        }

        protected virtual void OnUpdated()
        {
        }

        /// <summary>
        /// 创建渲染队列
        /// </summary>
        /// <param name="threaded"></param>
        public void CreateRenderQueue(bool threaded)
        {
            RenderingPipielineCore.CreateRenderingQueue(this, threaded);
        }

        /// <summary>
        /// 重置徐然队列密度
        /// </summary>
        public void ResetDensity()
        {
            RenderingQueue renderingQueue;

            if (m_RenderingQueue == null)
            {
                return;
            }

            for (int i = 0; i < m_RenderingQueue.Length; i++)
            {
                renderingQueue = m_RenderingQueue[i];
                renderingQueue.UpdateDensities();
            }
        }
    }
}
